package com.cskaoyan.market.service.impl;

import com.cskaoyan.market.db.domain.MarketRegion;
import com.cskaoyan.market.db.domain.MarketRegionExample;
import com.cskaoyan.market.db.mapper.MarketRegionMapper;
import com.cskaoyan.market.service.AdminRegionService;
import com.cskaoyan.market.util.Constant;
import com.cskaoyan.market.util.MybatisUtil;
import com.cskaoyan.market.vo.MarketRegionVO;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AdminRegionServiceImpl implements AdminRegionService {
    @Override
    public List<MarketRegionVO> list() {
        SqlSession session = MybatisUtil.getSession();
        MarketRegionMapper regionMapper = session.getMapper(MarketRegionMapper.class);
        //里面包含了省、市、县全部的数据
        List<MarketRegion> regionList = regionMapper.selectByExample(new MarketRegionExample());
        session.commit();
        session.close();
        // 将省、市、县数据加以分类，让其具有层级关系
        Map<Integer,List<MarketRegion>> regionGroupByPidMap=groupByPid(regionList);
        List<MarketRegion> provinceList=regionGroupByPidMap.get(Constant.PROVINCE_TYPE);
        List<MarketRegionVO> provinceVOList=new ArrayList<>();
        for (MarketRegion province : provinceList) {
            MarketRegionVO provinceVO = new MarketRegionVO(province.getId(), province.getName(), province.getType(), province.getCode(), null);
            // 查找当前省下面的城市信息
            List<MarketRegionVO> cityVoList=new ArrayList<>();
            List<MarketRegion> cityList=regionGroupByPidMap.get(province.getId());
            for (MarketRegion city : cityList) {
                MarketRegionVO cityVo=new MarketRegionVO(city.getId(),city.getName(),city.getType(),city.getCode(),null);
                List<MarketRegionVO> areaVoList=new ArrayList<>();
                List<MarketRegion> areaList=regionGroupByPidMap.get(city.getId());
                for (MarketRegion area : areaList) {
                    MarketRegionVO areaVo = new MarketRegionVO(area.getId(), area.getName(), area.getType(), area.getCode(), null);
                    areaVoList.add(areaVo);
                }
                cityVo.setChildren(areaVoList);
                cityVoList.add(cityVo);
            }
            provinceVO.setChildren(cityVoList);
            provinceVOList.add(provinceVO);
        }
        return provinceVOList;
    }

    // 将省、市、县分开
    private Map<Integer, List<MarketRegion>> groupByPid(List<MarketRegion> regionList) {
        Map<Integer, List<MarketRegion>> regionGroupByPidMap=new HashMap<>();
        for (MarketRegion marketRegion : regionList) {
            Integer pid=marketRegion.getPid();
            List<MarketRegion> regionListOfPid=regionGroupByPidMap.get(pid);
            if(regionListOfPid==null){
                regionListOfPid=new ArrayList<>();
                regionGroupByPidMap.put(pid,regionListOfPid);
            }
            regionListOfPid.add(marketRegion);
        }
        return regionGroupByPidMap;
    }
}
